<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Auth extends Controller
{
    //后台登录
    public function login()
    {
        if ($this->request->isAjax()) {
            $data = input('');
            // dump($data);
            $vercode = $data['vercode'];
            if (!captcha_check($vercode)) {
                $this->error('验证失败');

            }
            $username = trim($data['username']);
            $password = trim($data['password']);
            if (!$username || !$password) {
                $this->error('数据异常');
            }

            $user = Db::name('admin')->where('username', $username)->find();
            if (!$user || $user['status'] != 1) {
                $this->error('用户不存在 或状态异常');
            }
            if ($user['password'] != cmf_password($password)) {
                $this->error('验证失败');
            }
            session('admin_id', $user['id']);
            session('admin_info', $user);
            $updata = [];
            $updata['last_time'] = time();
            $updata['last_ip'] = request()->ip();

            Db::name('admin')->where('id', $user['id'])->update($updata);


            $this->success('验证通过', '', ['access_token' => request()->token()]);
        }

        return $this->fetch();
    }


    public function logout()
    {

        session('admin_id', null);
        session('admin_info', null);
        return json(['code' => 1, 'msg' => '退出成功']);

    }


}
